package com.qianniu.napi.admin.web.goods.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;

import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.User;

import com.qianniu.napi.admin.web.goods.entity.GoodsInternetProduct;
import com.qianniu.napi.admin.web.goods.service.IGoodsInternetProductService;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.helper.HttpHelper;
import com.qianniu.napi.common.util.EncryptUtil;
import com.qianniu.napi.common.util.ObjectUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>
 * 商品子表网红单品
 * </p>
 *
 * @author hefl
 * @since 2023-12-16
 */
@Controller
public class GoodsInternetProductController {

    private static final Logger logger = LoggerFactory.getLogger(GoodsInternetProductController.class);

    @Autowired
    private IGoodsInternetProductService goodsinternetproductService;

    @RequestMapping("admin/goodsinternetproduct/index")
    public String index() {
        return "admin/goodsinternetproduct";
    }

    @RequestMapping("/admin/goodsinternetproduct/list")
    @ResponseBody
    public Page<GoodsInternetProduct> list(@RequestParam(value="limit",required = false) int limit,
                                           @RequestParam(value="offset",required = false) int offset) {
        Wrapper<GoodsInternetProduct> wrapper = new EntityWrapper<GoodsInternetProduct>().eq("mpid",ObjectUtil.getMpid());
        wrapper.orderBy("id",true);
        Page<GoodsInternetProduct> page = goodsinternetproductService.selectPage(new Page<GoodsInternetProduct>(offset/limit+1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/goodsinternetproduct/add")
    public String add(@Valid GoodsInternetProduct goodsinternetproduct) {

        goodsinternetproduct.setCreateTime(new Date());

        goodsinternetproduct.insert();
        return "admin/goodsinternetproduct";
    }

    @RequestMapping("/admin/goodsinternetproduct/getGoodsInternetProductById")
    @ResponseBody
    public GoodsInternetProduct getGoodsInternetProductById(long id) {
        GoodsInternetProduct goodsinternetproduct = goodsinternetproductService.selectById(id);
        return goodsinternetproduct;
    }

    @RequestMapping("/admin/goodsinternetproduct/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = goodsinternetproductService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/goodsinternetproduct/changeState")
    @ResponseBody
    public String changeState(long id, int state) {
        GoodsInternetProduct goodsinternetproduct = new GoodsInternetProduct();
        goodsinternetproduct.setId(id);
        goodsinternetproduct.setState(state);
        goodsinternetproductService.updateById(goodsinternetproduct);
        return "success";
    }

    @RequestMapping("/admin/goodsinternetproduct/modify")
    public String update(GoodsInternetProduct goodsinternetproduct) {
        boolean isSuccc = goodsinternetproductService.updateById(goodsinternetproduct);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

    /// / ************* bellow is goodsinternetproduct interface for user *********

    @RequestMapping("/api/goodsinternetproduct/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value="status",required = false)String status,
                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if(pageSize==null)pageSize = 10;
        Page page=new Page(pageIndex,pageSize);
        page = goodsinternetproductService.selectPage(page, new EntityWrapper<GoodsInternetProduct>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/goodsinternetproduct/add")
    @ResponseBody
    public Jinfo goodsinternetproductAdd(@Valid GoodsInternetProduct goodsinternetproduct,
                          BindingResult bindingResult,HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if(bindingResult.hasErrors())
        {
            logger.debug( "There are errors! {}", bindingResult );
            info.setMsg("There are errors! "+bindingResult);
            info.setCode(400);
            return info;
        }
        goodsinternetproduct.setCreateTime(new Date());
        goodsinternetproduct.insert();
        return info;
    }

    @RequestMapping("/api/goodsinternetproduct/modify")
    @ResponseBody
    public Jinfo goodsinternetproductUpdate(HttpServletRequest req,
                                    GoodsInternetProduct goodsinternetproduct) {
        Jinfo info = new Jinfo();
        goodsinternetproduct.setUpdatedTime(new Date());
        goodsinternetproduct.updateById();
        return info;
    }

    @RequestMapping("/api/goodsinternetproduct/detail")
    @ResponseBody
    public Jinfo getGoodsInternetProductDetailById(@RequestParam("token") String token,
                                       @RequestParam("id") long id,HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        GoodsInternetProduct goodsinternetproduct = goodsinternetproductService.selectById(id);
        info.setInfo(goodsinternetproduct);
        return info;
    }
}
